﻿using Common;
using Common.Cache;
using Common.DataAnnotations;
using Common.Json;
using Common.Web;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Sample.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    //[Cached(60)]
    public class TestController : ControllerBase
    {
        //private readonly IRedisCache cache;
        //public TestController(IRedisCache cache)
        //{
        //    this.cache = cache;
        //}
        [HttpPost]
        public IActionResult Post(TestData data)
        {
            return Ok(data);
        }
        [HttpGet]
        [Authorize]
        public FuncResult<TestResponseDTO> Get()
        {
            return FuncResult.Success(new TestResponseDTO
            {
                OfferType = OfferType.一口价 | OfferType.自由报价,
                NullableOffer = OfferType.一口价,
                OfferArray = new OfferType[] { OfferType.一口价 },
                OfferList = new List<OfferType?> { OfferType.自由报价 },
                User = this.GetLoginUser()
            });
        }
        private readonly static HttpClient httpClient = new HttpClient { BaseAddress = new Uri("http://wallet.dev.zys6d.cn") };
        [HttpGet("data")]
        public FuncResult<TestData> GetTestData()
        {
            var queryRes = httpClient.GetAsync("/api/Query/ownertyper?id=1").ConfigureAwait(false).GetAwaiter().GetResult();
            string owners = queryRes.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
            Random r = new Random();
            var res = new TestData
            {
                SalesPrice = r.Next(1000, 1000000),
                CostPrice = r.Next(500, 200000),
                MobileNo = r.Next(1300000000, 1900000000).ToString(),
                OrderNo = DateTime.Now.ToString("yyyyMMddHHmmss"),
                OrderType = OfferType.自由报价.ToString() + owners,
                UserCode = r.Next(1300000000, 1900000000).ToString(),
            };
            return FuncResult.Success(res);
        }
        [HttpGet("enum"), AllowAnonymous]
        public FuncResult<OfferType> TestResponse([Required(ErrorMessage = "{0}不允许为空"), Display(Name = "报价方式")] OfferType offer)
        {
            return offer;
        }
    }
    [Flags]
    public enum OfferType
    {
        [StringValue("freeprice")]
        自由报价 = 1,
        [StringValue("fixprice")]
        一口价 = 2
    }
    public class TestResponseDTO
    {
        public OfferType OfferType { get; set; }
        public OfferType? NullableOffer { get; set; }
        public OfferType[] OfferArray { get; set; }
        public List<OfferType?> OfferList { get; set; }
        public LoginUser User { get; set; }
    }
    public class TestData
    {
        [DependencyRequired(nameof(OrderType), "remit", "payment")]
        [Display(Name = "Order NO.")]
        public string OrderNo { get; set; }
        [Display(Name = "售价")]
        public decimal SalesPrice { get; set; }
        [CompareTo(nameof(SalesPrice), CompareToAttribute.CompareOptions.GreatThanOrEquals), Display(Name = "成本价")]
        public decimal CostPrice { get; set; }
        [EnumDefined(typeof(OfferType))]
        public string OrderType { get; set; }
        [OneRequired("user")]
        public string MobileNo { get; set; }
        [OneRequired("user"), JsonConverter(typeof(StringMaskConverter))]
        public string UserCode { get; set; }
        public string DefinitionCode { get { throw new ArgumentException(); } }
    }
}
